package cn.bearspark.happening.provider.dao.key;

import cn.bearspark.framework.redis.starter.buffer.hash.bi.RedisBiHashBufferKeyBuilder;
import cn.bearspark.framework.redis.starter.buffer.hash.un.RedisHashBufferKeyBuilder;
import cn.bearspark.framework.redis.starter.key.RedisKeyBuilder;
import org.springframework.stereotype.Component;

/**
 * 动态的投票模块中 redis 缓存键的构造器
 *
 * @author f
 */
@Component
public class VoteBufferKeyBuilder extends RedisKeyBuilder
        implements RedisHashBufferKeyBuilder, RedisBiHashBufferKeyBuilder {

    /**
     * 投票的信息 的缓存的键的前缀
     */
    private static final String VOTE_INFO = "voteInfo";

    /**
     * 投票选项的信息 的缓存的键的前缀
     */
    private static final String VOTE_OPTION_INFO = "voteOptionInfo";

    /**
     * 投票选项 id 的缓存的键的前缀
     */
    private static final String VOTE_OPTION_ID = "voteOptionId";

    /**
     * 投票 id 和 用户 id 的缓存的键的前缀
     */
    private static final String VOTE_USER_ID = "voteUserId";

    @Override
    public String buildHashBufferKey(long voteId) {
        return super.getPrefix() + VOTE_INFO + super.getSplitItem() + voteId;
    }

    @Override
    public String buildBiHashBufferKey(long voteId, long optionId) {
        return super.getPrefix() + VOTE_OPTION_INFO +
               super.getSplitItem() + voteId +
               super.getSplitItem() + optionId;
    }

    @Override
    public String buildBiHashBufferKeyPattern(long voteId) {
        return super.getPrefix() + VOTE_OPTION_INFO + super.getSplitItem() + voteId + "*";
    }

    public String buildVoteOptionIdListKey(long voteId) {
        return super.getPrefix() + VOTE_OPTION_ID + super.getSplitItem() + voteId;
    }

    public String buildVoteUserIdKey(long voteId, long userId, int visCode) {
        return super.getPrefix() + VOTE_USER_ID +
               super.getSplitItem() + voteId +
               super.getSplitItem() + userId +
               super.getSplitItem() + visCode;
    }

    public String buildVoteUserIdKeyPattern(long voteId) {
        return super.getPrefix() + VOTE_USER_ID + super.getSplitItem() + voteId + "*";
    }
}
